package server;

import control.CControl;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import websocket.Framedata;
import websocket.WebSocketConnection;
import websocket.WebSocketListener;

public class SClient implements WebSocketListener
{
    private final static Logger LOGGER = Logger.getLogger(SClient.class
        .getName());
    
    private boolean isHighPriority = false;
    private CControl cControl;
    private WebSocketConnection webSocket;
    
    public SClient (CControl cControl, WebSocketConnection wsConnection)
    {
        if (wsConnection == null || cControl == null)
        {
            throw new IllegalArgumentException("Null arguments passed to SClient constructor");
        }
        
        webSocket = wsConnection;
        webSocket.setListener(this);
        
        
        if ("127.0.0.1".equals(webSocket.getInetAddress()))
        {
            isHighPriority = true;
        }
        
        this.cControl = cControl;
        cControl.addClient(this);
    }
    
    public boolean isConnected()
    {
        return webSocket.getState() == WebSocketConnection.State.OPEN;
        //return true;
    }
    
    public int getPort()
    {
        return webSocket.getPort();
    }
    
    public String getInetAddress()
    {
        return webSocket.getInetAddress();
    }
    
    public String getClientName()
    {
        StringBuilder sb = new StringBuilder(getInetAddress().toString());
        sb.append(":");
        sb.append(getPort());
        return sb.toString();
        
    }
    
    public void sendMessage(String message)
    {
        if (isConnected())
        {
            webSocket.sendMessage(message);
        }
    }
    
        public void sendMessage(Framedata[] frames)
    {
        if (isConnected())
        {
            webSocket.sendMessage(frames);
        }
    }
    
    public Boolean isHighPriority()
    {
        return isHighPriority;
    }
    
    @Override
    public void onMessage(String message)
    {
        // Just log that the message was received
        LOGGER.log(Level.FINE, "Message received from {0}:\n{1}", new Object[]{webSocket.getInetAddress(), message});
    }

    @Override
    public void onMessage(byte[] message)
    {
        LOGGER.log(Level.FINE, "Binary data received from {0}", webSocket.getInetAddress());
    }

    @Override
    public void onClosed()
    {
        LOGGER.log(Level.FINE, "Closed event on client {0} was received", getClientName());
        cControl.removeClient(this);
    }

    @Override
    public void onError()
    {
        LOGGER.log(Level.FINE, "Error event on client {0} was received", getClientName());
        cControl.removeClient(this);
    }

    @Override
    public void onBrokenConnection()
    {
        LOGGER.log(Level.FINE, "Broken connection event on client {0} was received", getClientName());
        cControl.removeClient(this);
    }
}
